const sequelize = require('./config.js')
const userledger = require('./model/user.js')
const salesmanInfo = require('./model/salesman_info.js')
const merchantInfo = require('./model/merchant_info')
const reservation = require('./model/reservation.js')
const productService = require('./model/product_service.js')
const memberInfo = require('./model/member_info.js')
const appoint = require('./model/appoint.js')


salesmanInfo.belongsTo(userledger,{foreignKey: 'user_id'}); // salesmanInfo 属于 userledger
merchantInfo.belongsTo(userledger, {
	foreignKey: 'user_id',
	as: 'userInfo'  // 商家对应的用户信息
  });
  userledger.hasOne(userledger, {  // A 有一个 B
	foreignKey: 'Id',
	sourceKey: 'creator_id',
	as: 'creatorInfo'  // 创建者信息
  });
  productService.belongsTo(salesmanInfo,{foreignKey:'merchant_id'})
  memberInfo.belongsTo(userledger,{foreignKey: 'user_id'});
  productService.hasMany(appoint, {
    foreignKey: 'service_id',  // 关联到 appoint 表的 service_id
    sourceKey: 'Id'            // 关联到 productService 表的 Id
});
// reservation.hasMany(UserInfo)

appoint.belongsTo(productService, {
    foreignKey: 'service_id', // appoint 中的 service_id 字段
    targetKey: 'Id', // productService 中的 Id 字段
    as: 'productService' // 关联的别名
});
appoint.belongsTo(memberInfo, {
    foreignKey: 'user_id', // appoint 中的 service_id 字段
    targetKey: 'user_id', // productService 中的 Id 字段
});
// appoint.hasMany(memberInfo)
// appoint.hasMany(merchantInfo)

module.exports = {
	sequelize,
	userledger,
	salesmanInfo,
	merchantInfo,
	// reservation,
	productService,
	memberInfo,
	appoint
}